#include <stdlib.h>
#include "mylib.h"

void drawRect(int x, int y, int width, int height, vu16 color);

int main(void)
{
    int i, j;
    REG_DISPCNT = MODE3 | BG2_ENABLE;
			
    while (1)
    {
    		waitForVblank();
        

        for (i = 0; i < 20; i++)
        {
           	for (j = 0; j < 30; j++)
           	{
           			int index = i * 30 + j;
           			
           			/* Try to figure out what this line of code is doing */
           			/* Note ? : is the conditional operator much like in java it is equivalent to an if else where
           			 condition ? iftruethishappens : iffalsethishappens
           			 */
           			 /* ^ is bitwise xor */
           			drawRect(j * 8, i * 8, 8, 8, ((index & 1) ^ (i & 1)) ? RGB(0, 0, 0) : RGB(31, 31, 31));
           	}
        }
    }
}

/* Draws a rectangle */
/* Upon success when the game is ran a checkboard should appear */
void drawRect(int x, int y, int width, int height, vu16 color)
{
	/* Resist the temptation to look at Bills code here
	Although bill has given you this function you should work it out for yourself
	NO CODE IS GIVEN TO YOU FOR FREE YOU MUST UNDERSTAND HOW IT WORKS!!!
	The only exception to this is the drawLine function I have given you */
	int r;
	for(r=0; r<height; r++)
	{
		REG_DMA3SAD = (vu32)&color;
		REG_DMA3DAD = (vu32)&videoBuffer[((y+r)*(240)+(x))];
        REG_DMA3CNT = width | DMA_ON | DMA_SOURCE_FIXED;
	}
}

